<?php

namespace app\admin\controller;

use app\common\model\PropsNewModel;
use think\facade\Request;
use think\Validate;

class PropsController extends BaseController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = PropsNewModel::select();
            foreach ($list as &$item) {
                $item->type_text = $item->type_text;
            }

            return json(["code" => 0, "msg" => "", "list" => $list, "count" => count($list)]);
        }
        return $this->fetch();
    }

    public function edit()
    {
        if (!$id = $this->get('id', '')) {
            return static::bulidFail('ID不能为空');
        }

        if (!$model = PropsNewModel::where('id', $id)->find()) {
            return static::bulidFail('不存在的道具');
        }

        if ($this->request->isAjax()) {
            $all_param = Request::param();
            $validate  = new Validate([
                'name'   => 'require|length:1,10',
                'type'   => 'require|in:' . implode(',', array_keys(PropsNewModel::TYPE_CATE)),
                'icon'   => 'require|url',
                'remark' => 'max:255',
            ], [
                "name"   => "道具名称必填且不超过10个字符",
                "type"   => "功能属性错误",
                "icon"   => "道具图标地址错误",
                "remark" => "道具说明不超过255个字符",
            ]);

            if (!$validate->check($all_param)) {
                return self::bulidFail($validate->getError());
            }

            $model->name   = $all_param['name'];
            $model->type   = $all_param['type'];
            $model->icon   = $all_param['icon'];
            $model->remark = $all_param['remark'];

            if (!$model->save()) {
                return static::bulidFail();
            }

            // 记录日志
            $log_content = "修改道具，ID：{$model->id}，名称：{$model->name}";
            parent::addAdminLog($log_content);

            return static::bulidSuccess();
        }

        return $this->assign([
            'list'      => $model->toArray(),
            'type_cate' => PropsNewModel::TYPE_CATE,
        ])->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $all_param = Request::param();
            $validate  = new Validate([
                'name'   => 'require|length:1,10',
                'type'   => 'require|in:' . implode(',', array_keys(PropsNewModel::TYPE_CATE)),
                'icon'   => 'require|url',
                'remark' => 'max:255',
            ], [
                "name"   => "道具名称必填且不超过10个字符",
                "type"   => "功能属性错误",
                "icon"   => "道具图标地址错误",
                "remark" => "道具说明不超过255个字符",
            ]);

            if (!$validate->check($all_param)) {
                return self::bulidFail($validate->getError());
            }

            $model = new PropsNewModel($all_param);

            if (!$model->save()) {
                return static::bulidFail();
            }

            // 记录日志
            $log_content = "添加道具，ID：{$model->id}，名称：{$model->name}";
            parent::addAdminLog($log_content);
            return static::bulidSuccess();
        }
        return $this->assign([
            'type_cate' => PropsNewModel::TYPE_CATE,
        ])->fetch();
    }

    public function status()
    {
        if (!$id = $this->post('id', '')) {
            return static::bulidFail('ID不能为空');
        }

        if (!in_array($status = $this->post('status', ''), ['0', 1])) {
            return static::bulidFail();
        }

        if (!$model = PropsNewModel::where('id', $id)->find()) {
            return static::bulidFail('不存在的道具');
        }

        $model->status = $status;
        if (!$model->save()) {
            return static::bulidFail();
        }

        // 记录日志
        $log_content = "设置道具" . $status == 1 ? '上架' : '下架' . "，ID：{$model->id}，名称：{$model->name}";
        parent::addAdminLog($log_content);
        return static::bulidSuccess();
    }
}